﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbobsCOM;
using System.IO;

namespace CompanySyncProject
{
    public class Importer
    {
        public static SAPbobsCOM.Company oCompany;
        public static System.IO.StreamWriter logtrace;

        public Importer(SAPbobsCOM.Company cmp, System.IO.StreamWriter log)
        {
            oCompany = cmp;
            logtrace = log;
        }

        static void logTrace(string inputStr)
        {
            {
                logtrace.WriteLine(inputStr);
                logtrace.Flush();
            }
        }

        public static void Import(string folder)
        {
            ImportWarehouse(folder);
            ImportSalesPeople(folder);
            ImportPaymentTerms(folder);
            ImportShippingType(folder);
            ImportItem(folder);
            AmendItem();
            ImportBP(folder);
        }

        public static void ImportWarehouse(string folder)
        {
            string code = string.Empty;
            bool found = false;
            List<string> warehouseList = new List<string>();
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            const string queryStr = @"select WhsCode from [OWHS]";
            oRec.DoQuery(queryStr);
            for (int i = 0; i < oRec.RecordCount; i++)
            {
                code = oRec.Fields.Item(0).Value;
                warehouseList.Add(code);
                oRec.MoveNext();
            }

            string filepath = string.Empty;
            string exactFolder = String.Format(@"c:\temp\{0}\warehouse", folder);
            SAPbobsCOM.IWarehouses owhs = null;
            int ret = 0;
            string errMsg = string.Empty;

            System.IO.DirectoryInfo directory = new DirectoryInfo(exactFolder);
            FileInfo[] fileList = directory.GetFiles();
            for (int i = 0; i < fileList.Length; i++)
            {
                filepath = fileList[i].FullName;
                owhs = oCompany.GetBusinessObjectFromXML(filepath, 0);
                code = owhs.WarehouseCode;

                for (int j = 0; j < (warehouseList.Count); j++)
                {
                    if (warehouseList[j].Equals(code))
                    {
                        /*
                        owhs.GetByKey(code);
                        owhs.Browser.ReadXml(filepath, 0);

                        ret = owhs.Update();
                        if (ret == 0)
                        {
                            logTrace(String.Format(@"Update: Warehouse import succeeded. Code: {0}", code));
                        }
                        else
                        {
                            errMsg = oCompany.GetLastErrorDescription();
                            logTrace(String.Format(@"Update: Warehouse import failed. Code: {0}, Reason: {1}", code, errMsg));
                        }
                         */ 
                        found = true;
                        break;
                    }
                }

                if (found == false)
                {
                    ret = owhs.Add();
                    if (ret == 0)
                    {
                        logTrace(String.Format(@"Add: Warehouse import succeeded. Code: {0}", code));
                    }
                    else
                    {
                        errMsg = oCompany.GetLastErrorDescription();
                        logTrace(String.Format(@"Add: Warehouse import failed. Code: {0}, Reason: {1}", code, errMsg));
                    }
                }
                found = false;
            }
        }


        static void ImportItem(string folder)
        {
            string code = string.Empty;
            bool found = false;
            List<string> itemList = new List<string>();
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            const string queryStr = @"select itemCode from [OITM]";
            oRec.DoQuery(queryStr);
            for (int i = 0; i < oRec.RecordCount; i++)
            {
                code = oRec.Fields.Item(0).Value;
                itemList.Add(code);
                oRec.MoveNext();
            }

            string filepath = string.Empty;
            string exactFolder = String.Format(@"c:\temp\{0}\item", folder);
            SAPbobsCOM.IItems oitm = null;
            int ret = 0;
            string errMsg = string.Empty;
            //bool getbykey = false;

            System.IO.DirectoryInfo directory = new DirectoryInfo(exactFolder);
            FileInfo[] fileList = directory.GetFiles();
            for (int i = 0; i < fileList.Length; i++)
            {
                filepath = fileList[i].FullName;
                oitm = oCompany.GetBusinessObjectFromXML(filepath, 0);
                code = oitm.ItemCode;
                
                for (int j = 0; j < (itemList.Count); j++)
                {
                    if (itemList[j].Equals(code))
                    {
                        /*
                        SAPbobsCOM.IItems oitemUpdate = (SAPbobsCOM.IItems)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems);
                        getbykey = oitemUpdate.GetByKey(code);
                        oitemUpdate.Browser.ReadXml(filepath, 0);


                        ret = oitemUpdate.Update();
                        if (ret == 0)
                        {
                            logTrace(String.Format(@"Update: Item import succeeded. Code: {0}", code));
                        }
                        else
                        {
                            errMsg = oCompany.GetLastErrorDescription();
                            logTrace(String.Format(@"Update: Item import failed. Code: {0}, Reason: {1}", code, errMsg));
                        }*/
                        found = true;
                        break;
                    }
                }
                
                if (found == false)
                {
                    /*
                    //882
                    oitm.Series = 3;
                    */
                    ret = oitm.Add();
                    if (ret == 0)
                    {
                        logTrace(String.Format(@"Add: Item import succeeded. Code: {0}", code));
                    }
                    else
                    {
                        errMsg = oCompany.GetLastErrorDescription();
                        logTrace(String.Format(@"Add: Item import failed. Code: {0}, Reason: {1}", code, errMsg));
                    }
                }
                found = false;
            }
        }

        static void AmendItem()
        {
            const string queryStr = @"update oitw set avgprice = 0 where OnHand = 0 and IsCommited = 0 and OnOrder = 0 and AvgPrice <> 0";
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            oRec.DoQuery(queryStr);
        }

        static void ImportShippingType(string folder)
        {
            string code = string.Empty;
            bool found = false;
            List<string> shtyList = new List<string>();
            List<int> shtyCodeList = new List<int>();
            int code2;
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            const string queryStr = @"select trnspName, trnspCode from [OSHP]";
            oRec.DoQuery(queryStr);
            for (int i = 0; i < oRec.RecordCount; i++)
            {
                code = oRec.Fields.Item(0).Value;
                shtyList.Add(code);
                code2 = oRec.Fields.Item(1).Value;
                shtyCodeList.Add(code2);
                oRec.MoveNext();
            }

            string filepath = string.Empty;
            string exactFolder = String.Format(@"c:\temp\{0}\shippingtype", folder);
            SAPbobsCOM.IShippingTypes oist = null;
            int ret = 0;
            string errMsg = string.Empty;

            System.IO.DirectoryInfo directory = new DirectoryInfo(exactFolder);
            FileInfo[] fileList = directory.GetFiles();
            for (int i = 0; i < fileList.Length; i++)
            {
                filepath = fileList[i].FullName;
                oist = oCompany.GetBusinessObjectFromXML(filepath, 0);
                code = oist.Code.ToString();
                
                for (int j = 0; j < (shtyList.Count); j++)
                {
                    if (shtyList[j].Equals(oist.Name))
                    {
                        /*
                        oist.GetByKey(shtyCodeList[j]);
                        oist.Browser.ReadXml(filepath, 0);

                        ret = oist.Update();
                        if (ret == 0)
                        {
                            logTrace(String.Format(@"Update: Shipping Type import succeeded. Code: {0}", code));
                        }
                        else
                        {
                            errMsg = oCompany.GetLastErrorDescription();
                            logTrace(String.Format(@"Update: Shipping Type import failed. Code: {0}, Reason: {1}", code, errMsg));
                        }
                         */ 
                        found = true;
                        break;
                    }
                }

                if (found == false)
                {
                    ret = oist.Add();
                    if (ret == 0)
                    {
                        logTrace(String.Format(@"Add: Shipping Type import succeeded. Code: {0}", code));
                    }
                    else
                    {
                        errMsg = oCompany.GetLastErrorDescription();
                        logTrace(String.Format(@"Add: Shipping Type import failed. Code: {0}, Reason: {1}", code, errMsg));
                    }
                }
                found = false;
            }
        }

        static void ImportBP(string folder)
        {
            string code = string.Empty;
            bool found = false;
            List<string> BPList = new List<string>();
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            const string queryStr = @"select CardCode from [OCRD]";
            oRec.DoQuery(queryStr);
            for (int i = 0; i < oRec.RecordCount; i++)
            {
                code = oRec.Fields.Item(0).Value;
                BPList.Add(code);
                oRec.MoveNext();
            }

            string filepath = string.Empty;
            string exactFolder = String.Format(@"c:\temp\{0}\BP", folder);
            SAPbobsCOM.IBusinessPartners ocrd = null;
            int ret = 0;
            string errMsg = string.Empty;

            System.IO.DirectoryInfo directory = new DirectoryInfo(exactFolder);
            FileInfo[] fileList = directory.GetFiles();
            for (int i = 0; i < fileList.Length; i++)
            {
                filepath = fileList[i].FullName;
                ocrd = oCompany.GetBusinessObjectFromXML(filepath, 0);
                code = ocrd.CardCode;

                for (int j = 0; j < (BPList.Count); j++)
                {
                    if (BPList[j].Equals(code))
                    {
                        /*
                        ocrd.GetByKey(code);
                        ocrd.Browser.ReadXml(filepath, 0);

                        ret = ocrd.Update();
                        if (ret == 0)
                        {
                            logTrace(String.Format(@"Update: BP import succeeded. Code: {0}", code));
                        }
                        else
                        {
                            errMsg = oCompany.GetLastErrorDescription();
                            logTrace(String.Format(@"Update: BP import failed. Code: {0}, Reason: {1}", code, errMsg));
                        }
                         */
                        found = true;
                        break;
                    }
                }

                if (found == false)
                {
                    /*
                    //882
                    if (ocrd.CardType == BoCardTypes.cSupplier)
                        ocrd.Series = 2;
                    else
                        ocrd.Series = 1;
                    */
                    ret = ocrd.Add();
                    if (ret == 0)
                    {
                        logTrace(String.Format(@"Add: BP import succeeded. Code: {0}", code));
                    }
                    else
                    {
                        errMsg = oCompany.GetLastErrorDescription();
                        logTrace(String.Format(@"Add: BP import failed. Code: {0}, Reason: {1}", code, errMsg));
                    }
                }
                found = false;
            }
        }

        static void ImportPaymentTerms(string folder)
        {
            string code = string.Empty;
            int code2 = 0;
            bool found = false;
            List<string> SlpNameList = new List<string>();
            List<int> SlpCodeList = new List<int>();
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            const string queryStr = @"select PymntGroup, GroupNum from [OCTG]";
            oRec.DoQuery(queryStr);
            for (int i = 0; i < oRec.RecordCount; i++)
            {
                code = oRec.Fields.Item(0).Value;
                SlpNameList.Add(code);
                code2 = oRec.Fields.Item(1).Value;
                SlpCodeList.Add(code2);
                oRec.MoveNext();
            }

            string filepath = string.Empty;
            string exactFolder = String.Format(@"c:\temp\{0}\PaymentTerms", folder);
            SAPbobsCOM.IPaymentTermsTypes octg = null;
            int ret = 0;
            string errMsg = string.Empty;

            System.IO.DirectoryInfo directory = new DirectoryInfo(exactFolder);
            FileInfo[] fileList = directory.GetFiles();
            for (int i = 0; i < fileList.Length; i++)
            {
                filepath = fileList[i].FullName;
                octg = oCompany.GetBusinessObjectFromXML(filepath, 0);
                code = octg.PaymentTermsGroupName;
                code2 = octg.GroupNumber;

                for (int j = 0; j < (SlpNameList.Count); j++)
                {
                    if (SlpNameList[j].Equals(code))
                    {
                        /*
                        code2 = SlpCodeList[j];
                        octg.GetByKey(code2);
                        octg.Browser.ReadXml(filepath, 0);

                        ret = octg.Update();
                        if (ret == 0)
                        {
                            logTrace(String.Format(@"Update: PaymentTerms import succeeded. Code: {0}", code));
                        }
                        else
                        {
                            errMsg = oCompany.GetLastErrorDescription();
                            logTrace(String.Format(@"Update: PaymentTerms import failed. Code: {0}, Reason: {1}", code, errMsg));
                        }
                         */
                        found = true;
                        break;
                    }
                }

                if (found == false)
                {
                    ret = octg.Add();
                    if (ret == 0)
                    {
                        logTrace(String.Format(@"Add: PaymentTerms import succeeded. Code: {0}", code));
                    }
                    else
                    {
                        errMsg = oCompany.GetLastErrorDescription();
                        logTrace(String.Format(@"Add: PaymentTerms import failed. Code: {0}, Reason: {1}", code, errMsg));
                    }
                }
                found = false;
            }
        }

        static void ImportSalesPeople(string folder)
        {
            string code = string.Empty;
            int code2 = 0;
            bool found = false;
            List<string> SlpNameList = new List<string>();
            List<int> SlpCodeList = new List<int>();
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            const string queryStr = @"select SlpName, SlpCode from [OSLP]";
            oRec.DoQuery(queryStr);
            for (int i = 0; i < oRec.RecordCount; i++)
            {
                code = oRec.Fields.Item(0).Value;
                SlpNameList.Add(code);
                code2 = oRec.Fields.Item(1).Value;
                SlpCodeList.Add(code2);
                oRec.MoveNext();
            }

            string filepath = string.Empty;
            string exactFolder = String.Format(@"c:\temp\{0}\SalesPeople", folder);
            SAPbobsCOM.ISalesPersons oslp = null;
            int ret = 0;
            string errMsg = string.Empty;

            System.IO.DirectoryInfo directory = new DirectoryInfo(exactFolder);
            FileInfo[] fileList = directory.GetFiles();
            for (int i = 0; i < fileList.Length; i++)
            {
                filepath = fileList[i].FullName;
                oslp = oCompany.GetBusinessObjectFromXML(filepath, 0);
                code = oslp.SalesEmployeeName;
                code2 = oslp.SalesEmployeeCode;

                for (int j = 0; j < (SlpNameList.Count); j++)
                {
                    if (SlpNameList[j].Equals(code))
                    {
                        /*
                        code2 = SlpCodeList[j];
                        oslp.GetByKey(code2);
                        oslp.Browser.ReadXml(filepath, 0);

                        ret = oslp.Update();
                        if (ret == 0)
                        {
                            logTrace(String.Format(@"Update: SalesPeople import succeeded. Code: {0}", code));
                        }
                        else
                        {
                            errMsg = oCompany.GetLastErrorDescription();
                            logTrace(String.Format(@"Update: SalesPeople import failed. Code: {0}, Reason: {1}", code, errMsg));
                        }
                         */
                        found = true;
                        break;
                    }
                }

                if (found == false)
                {
                    ret = oslp.Add();
                    if (ret == 0)
                    {
                        logTrace(String.Format(@"Add: SalesPeople import succeeded. Code: {0}", code));
                    }
                    else
                    {
                        errMsg = oCompany.GetLastErrorDescription();
                        logTrace(String.Format(@"Add: SalesPeople import failed. Code: {0}, Reason: {1}", code, errMsg));
                    }
                }
                found = false;
            }
        }

    }
}
